package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.mapper.SysUserMapper;
import com.sofwin.pojo.SysUser;
import com.sofwin.pojo.SysUserExample;
import com.sofwin.service.UserService;

import java.util.List;

/**
 * ClassName: UserServiceImpl
 * Description: TODO
 * date: 2022/4/16 10:52 上午
 *
 * @author andyliu
 * @qq 115176513
 * @since JDK 1.8
 */
public class UserServiceImpl implements UserService {
    private SysUserMapper mapper;
    public boolean saveOrUpdateUser(SysUser user) {
        boolean status = false;
        if(user.getId()!=null){
            status = mapper.updateByPrimaryKeySelective(user)==1?true:false;
            return status;
        }
        status = mapper.insertSelective(user)==1?true:false;
        return status;
    }

    public SysUser queryById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    public PageInfo<SysUser> queryUsers(SysUser user, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        SysUserExample userExample = new SysUserExample();
        SysUserExample.Criteria criteria = userExample.createCriteria();
        if(user!=null){
            if(user.getUserName()!=null && !"".equals(user.getUserName())){
                criteria.andUserNameLike("%"+user.getUserName()+"%");
            }
        }
        List<SysUser> users = mapper.selectByExample(userExample);
        return new PageInfo<SysUser>(users);
    }

    public boolean removeUser(Integer id) {
        return mapper.deleteByPrimaryKey(id)==1?true:false;
    }

    public void setMapper(SysUserMapper mapper) {
        this.mapper = mapper;
    }
}
